package com.example.demo.util;

import cn.hutool.core.codec.Base64;
import cn.hutool.crypto.Mode;
import cn.hutool.crypto.Padding;
import cn.hutool.crypto.symmetric.AES;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.symmetric.SymmetricAlgorithm;
import org.apache.logging.log4j.util.Base64Util;

public class TokenUtil {
    public static String aes_secretkey="2123sdfzgb234sbg";
    private static String TOKEN_SPLIT=":";
    //校验token是否有效
    public static TokenResultEnum isValidToken(String token) {
        return TokenResultEnum.SUCCESS;
    }
    //根据token获取用户 unionUuid全局唯一编码
    public static String getUserUuidFromToken(String token) {
        return "userUuid";
    }

    //刷新token
    public static String refreshToken(String token) {
        return "token";
    }

    public static String generatorToken(String openid, String unionid, String sessionkey) {
        AES aes = SecureUtil.aes(aes_secretkey.getBytes());
        StringBuilder sb=new StringBuilder();;
        sb.append(openid).append(TOKEN_SPLIT).append(unionid).append(TOKEN_SPLIT).append(sessionkey);
        return  aes.encryptBase64(sb.toString());
    }

    public static EasyToken parseToken(String token) {
        AES aes =SecureUtil.aes(aes_secretkey.getBytes());
        byte[] decrypt = aes.decrypt(token);
        String str=new String(decrypt);
        String[] split = str.split(":");
        return new EasyToken(split[0],split[1],split[2]);
    }
}
